package com.sun;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * https://leetcode.cn/problems/integer-to-roman/
 *给你一个整数，将其转为罗马数字。
 * 1 <= num <= 3999
 */
public class Test12整数转罗马数字 {
    public static void main(String[] args) {
        String s = intToRoman3(1000);
        System.out.println(s);

    }
    /**
     * 方法一：自己
     */
    public static String intToRoman1(int num){
        HashMap<Integer,String> map = new HashMap<>(){
            {
                put(1,"I");
                put(2,"II");
                put(3,"III");
                put(4,"IV");
                put(5,"V");
                put(6,"VI");
                put(7,"VII");
                put(8,"VIII");
                put(9,"IX");
                put(10,"X");
                put(40,"XL");
                put(50,"L");
                put(90,"XC");
                put(100,"C");
                put(400,"CD");
                put(500,"D");
                put(900,"CM");
                put(1000,"M");
            }
        };
        if (map.keySet().contains(num)){
            return map.get(num);
        }
        int temp = 1;
        List<String> resList = new ArrayList<>();
        while (num > 0){
            int rest =  num % 10;
            int restTemp = rest * temp;
            if (map.get(restTemp) != null){
                resList.add(map.get(restTemp));
            }else if (rest <= 3){
                StringBuilder stringBuilder = new StringBuilder();
                for (int i = 0; i < rest; i++) {
                    stringBuilder.append(map.get(temp));
                }
                resList.add(stringBuilder.toString());
            }else if (5 < rest && rest < 9){
                StringBuilder stringBuilder = new StringBuilder(map.get(temp*5));
                for (int i = 0; i < rest - 5; i++) {
                    stringBuilder.append(map.get(temp));
                }
                resList.add(stringBuilder.toString());
            }
            temp *= 10;
            num /= 10;
        }
        StringBuilder res = new StringBuilder();
        for (int i = resList.size() - 1; i >= 0; i--) {
            res.append(resList.get(i));
        }
        return res.toString();
    }

    /**
     * 方法二：自己优化方法1
     */
    public static String intToRoman2(int num){
        HashMap<Integer,String> map = new HashMap<>(){
            {
                put(1,"I");
                put(2,"II");
                put(3,"III");
                put(4,"IV");
                put(5,"V");
                put(6,"VI");
                put(7,"VII");
                put(8,"VIII");
                put(9,"IX");
                put(10,"X");
                put(40,"XL");
                put(50,"L");
                put(90,"XC");
                put(100,"C");
                put(400,"CD");
                put(500,"D");
                put(900,"CM");
                put(1000,"M");
            }
        };
        if (map.keySet().contains(num)){
            return map.get(num);
        }
        int temp = 1;
        StringBuilder res = new StringBuilder();
        while (num > 0){
            int rest =  num % 10;
            int restTemp = rest * temp;
            if (map.get(restTemp) != null){
                res.insert(0,map.get(restTemp));
            }else if (rest <= 3){
                StringBuilder stringBuilder = new StringBuilder();
                for (int i = 0; i < rest; i++) {
                    stringBuilder.append(map.get(temp));
                }
                res.insert(0,stringBuilder);
            }else if (5 < rest && rest < 9){
                StringBuilder stringBuilder = new StringBuilder(map.get(temp*5));
                for (int i = 0; i < rest - 5; i++) {
                    stringBuilder.append(map.get(temp));
                }
                res.insert(0,stringBuilder);
            }
            temp *= 10;
            num /= 10;
        }
        return res.toString();
    }
    /**
     * 方法三：力扣官方，模拟
     */
    public static String intToRoman3(int num){
        int[] values = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
        String[] symbols = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
        StringBuilder roman = new StringBuilder();
        for (int i = 0; i < values.length; i++) {
            int value = values[i];
            String symbol = symbols[i];
            while (num >= value){
                num -= value;
                roman.append(symbol);
            }
        }
        return roman.toString();
    }
}
